<?php

class GlobalValidator {
	/**
	 * 检查用户名是否符合规定
	 *
	 * @param STRING $username 要检查的用户名
	 * @return 	TRUE or FALSE
	 */
	public function is_username($username){
		$strlen = strlen($username);
		if($this->is_badword($username) || !preg_match("/^[a-zA-Z0-9_][a-zA-Z0-9_]+$/",$username)){
			return false;		
		} elseif(20 < $strlen || $strlen <2 ){
			return false;		
		}
		return true;
	}
	
	/**
	 * 检测输入中是否含有错误字符
	 *
	 * @param char $string 要检查的字符串名称
	 * @return TRUE or FALSE
	 */
	public function is_badword($string){
		$badwords = array("\\",'&',' ',"'",'"','/','*',',','<','>',"\r","\t","\n","#");
		foreach($badwords as $word){
			if(strpos($string,$word) !==false){
				return true;
			}
		}
		return false;
	}
	/**
	 * 检查email
	 */
	public function is_email($email) {
		return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
	}
	
	/**
	 * 检查密码长度是否符合规定
	 *
	 * @param STRING $password
	 * @return 	TRUE or FALSE
	 */
	public function is_password($password){
		$strlen = strlen($password);
		if($strlen >= 6 && $strlen <= 20) return true;
		return false;
	}
}